# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

# petsc don't have clang build because it depends on fortran lib,
# so we have to disable clang for now

_realname=precice
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.3.0
pkgrel=3
pkgdesc='A Coupling Library for Partitioned Multi-Physics Simulations on Massively Parallel Systems (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
	 "${MINGW_PACKAGE_PREFIX}-msmpi"
	 "${MINGW_PACKAGE_PREFIX}-petsc"
	 "${MINGW_PACKAGE_PREFIX}-python-numpy")
makedepends=("${MINGW_PACKAGE_PREFIX}-petsc-build"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-cc"
	     "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake")
license=('LGPL3')
url='https://www.precice.org'
source=(https://github.com/precice/precice/archive/v${pkgver}.tar.gz)
sha256sums=('57BAB08E8B986F5FAA364689D470940DBD9C138E5CFA7B861793E7DB56B89DA3')

build() {
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  . ${MINGW_PREFIX}/src/petsc-*/petsc
  for build in ${petsc_builds}; do
    #export PETSC_ARCH=${build}
    export PETSC_ARCH=dmo
    export PETSC_DIR=${MINGW_PREFIX}/src/petsc-${petsc_pkgver}
  done

  #Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_CXX_STANDARD_LIBRARIES=-lws2_32 \
      -DBUILD_SHARED_LIBS=OFF \
      -DCMAKE_BUILD_TYPE=Release \
      -DPRECICE_MPICommunication=ON \
      -DPRECICE_PETScMapping=ON \
      -DPRECICE_PythonActions=ON \
      -DPC_PETSc_INCLUDE_DIRS="$PREFIX_WIN/src/petsc-${petsc_pkgver}/include;$PREFIX_WIN/src/petsc-${petsc_pkgver}/$PETSC_ARCH/include" \
      -Wno-dev \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_CXX_STANDARD_LIBRARIES=-lws2_32 \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_BUILD_TYPE=Release \
      -DPRECICE_MPICommunication=ON \
      -DPRECICE_PETScMapping=ON \
      -DPRECICE_PythonActions=ON \
      -DPC_PETSc_INCLUDE_DIRS="$PREFIX_WIN/src/petsc-${petsc_pkgver}/include;$PREFIX_WIN/src/petsc-${petsc_pkgver}/$PETSC_ARCH/include" \
      -Wno-dev \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR=${pkgdir} cmake --build . --target install	

  #Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR=${pkgdir} cmake --build . --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
